<?php


namespace app\store\controller\shop;

use app\store\model\CourseCard as CourseCardModel;
use app\store\controller\Controller;
use app\store\model\store\Shop as ShopModel;

/**
 * 课程类
 * Class Course
 * @package app\store\controller
 */
class CourseCard extends Controller
{
    public function index(){
        $model = new CourseCardModel();
        $shopList = $this->shopList();
        // 门店
        $use_type = $this->store['user']['user_type'];
        $shop_id = 0;
        if ($use_type == 2) {
            $shop_id = $this->store['user']['shop_user_id'];
        } elseif ($use_type == 3) { // 教练
            $coach_id = $this->store['user']['shop_user_id'];
            $detail = $model->field('shop_id')->find(['coach_id' => $coach_id]);
            $shop_id = $detail['shop_id'];
        }
        $list = $model->getList(array_merge(['status' => -1,'shop_id'=>$shop_id], $this->request->param()));
        // 商品分类
        return $this->fetch('index', compact('list','shopList'));
    }

    /**
     * 添加商品
     * @return array|mixed
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            $shopList=$this->shopList();
            return $this->fetch(
                'add',
                compact('shopList')
            );
        }
        $model = new CourseCardModel();
        if ($model->add($this->postData('goods'))) {
            return $this->renderSuccess('添加成功', url('shop.course_card/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 商品编辑
     * @param $goods_id
     * @return array|bool|mixed
     */
    public function edit($goods_id)
    {
        // 商品详情
        $model = CourseCardModel::detail($goods_id);
        if (!$this->request->isAjax()) {
            $shopList=$this->shopList();
            return $this->fetch(
                'edit',
                array_merge(compact('model','shopList'))
            );
        }
        // 更新记录
        if ($model->edit($this->postData('goods'))) {
            return $this->renderSuccess('更新成功', url('shop.course_card/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除商品
     * @param $goods_id
     * @return array
     */
    public function delete($goods_id)
    {
        // 商品详情
        $model = CourseCardModel::detail($goods_id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    public function state($goods_id, $state)
    {
        // 商品详情
        $model = CourseCardModel::detail($goods_id);
        if (!$model->setStatus($state)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    public function recommend($goods_id, $state)
    {
        // 商品详情
        $model = CourseCardModel::detail($goods_id);
        if (!$model->setRecommend($state)) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * // 门店列表
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function shopList(){
        $shop_id = $this->store['user']['shop_user_id'];
        $where=[];
        if ($shop_id){
            $where['shop_id']=$shop_id;
        }
        return ShopModel::getAllList($where);
    }
}